package qc.module.ehs.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.EhsHazardIndentificationResultTypeEnum;
import qc.common.core.utils.DateUtil;
import qc.module.ehs.dto.hazardidentity.HazardIdentifyInfoDto;
import qc.module.ehs.entity.EhsHazardIdentify;

/**
 * EhsHazardIdentifyMapper
 *
 * @author QuCheng Tech
 * @create 2024/11/5
 */
@Mapper
public interface EhsHazardIdentifyMapper {
    EhsHazardIdentifyMapper MAPPER = Mappers.getMapper(EhsHazardIdentifyMapper.class);

    //@Mapping(target = "tm", source = "tm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "tm", source = "tm", dateFormat = "yyyy-MM-dd")
    @Mapping(target = "intm", source = "intm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "confirmtm", source = "confirmtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "identificationresult", source = "identificationresult.index")
    //@Mapping(target = "confirmresult", source = "confirmresult.index")//确认结果不能直接映射转换，在数据库中值为null时转换后的值为0导致显示为不存在
    @Mapping(target = "confirmresult", ignore = true)
    HazardIdentifyInfoDto toDto(EhsHazardIdentify en);

    default HazardIdentifyInfoDto entityToDto(EhsHazardIdentify en) {
        HazardIdentifyInfoDto dto = EhsHazardIdentifyMapper.MAPPER.toDto(en);
        //判断确认结果是否为空，不为空时进行映射；同时要是否已确认为true并且确认结果不为空，否则判断为是没有进行确认        
        if (en.getHasconfirm() == true && en.getConfirmresult() != null)
            dto.setConfirmresult(en.getConfirmresult().getIndex());
        else//由于dto中属性数据类型为int，默认值为0，因此在为空时赋值为枚举中不存在的值
            dto.setConfirmresult(-1);

        return dto;
    }

    //辨识时间tm修改为在service中使用DateUtil中的方法进行转换
    @Mapping(target = "tm", ignore = true)
    //@Mapping(target = "tm", source = "tm", dateFormat = "yyyy-MM-dd")
    //@Mapping(target = "tm", source = "tm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "intm", source = "intm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "confirmtm", source = "confirmtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "identificationresult", ignore = true)
    @Mapping(target = "confirmresult", ignore = true)
    EhsHazardIdentify toEntity(HazardIdentifyInfoDto dto);

    default EhsHazardIdentify dtoToEntity(HazardIdentifyInfoDto dto) {
        EhsHazardIdentify en = EhsHazardIdentifyMapper.MAPPER.toEntity(dto);

        en.setTm(DateUtil.parseDate(dto.getTm()));
        en.setIdentificationresult(getEhsHazardIndentificationResultTypeEnum(dto.getIdentificationresult()));
        en.setConfirmresult(getEhsHazardIndentificationResultTypeEnum(dto.getConfirmresult()));

        return en;
    }

    static EhsHazardIndentificationResultTypeEnum getEhsHazardIndentificationResultTypeEnum(Integer index) {
        //包含状态为0-不存在
        if (index != null && index >= 0x0) {
            for (EhsHazardIndentificationResultTypeEnum item : EhsHazardIndentificationResultTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }

        //默认为null，辨识结果必须有
        return null;
    }
}
